package com.gitpractice.liwushuo.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.gitpractice.liwushuo.R;
import com.gitpractice.liwushuo.bean.hotBean;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.List;

/**
 * Created by admin on 2016/7/20.
 */
public class HotAdapter extends RecyclerView.Adapter<HotAdapter.HotHolder> {
    private Context mContext;
    private List<hotBean.ItemBean> mDatas;
    private DisplayImageOptions options;

    public HotAdapter(Context context, List<hotBean.ItemBean> datas) {
        this.mContext = context;
        this.mDatas = datas;
        options = new DisplayImageOptions.Builder()
                .showImageOnFail(R.mipmap.ic_launcher)
                .showImageOnLoading(R.mipmap.ic_launcher)
                .cacheOnDisk(true)
                .cacheInMemory(true)
                .bitmapConfig(Bitmap.Config.RGB_565)
                .build();
    }

    @Override
    public HotHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_hot, parent, false);
        return new HotHolder(view);
    }

    @Override
    public void onBindViewHolder(HotHolder holder, int position) {
        hotBean.ItemBean bean = mDatas.get(position);
        holder.tv_name.setText(bean.data.name);
        // Log.i("123","***tv_name***"+bean.data.name);
        holder.tv_price.setText(bean.data.price);
        // Log.i("123","***tv_price***"+bean.data.price);
        holder.tv_count.setText(bean.data.favorites_count);
        ImageLoader.getInstance().displayImage(bean.data.cover_image_url, holder.iv_main, options);
    }

    @Override
    public int getItemCount() {
        return mDatas == null ? 0 : mDatas.size();
    }

    class HotHolder extends RecyclerView.ViewHolder {
        private ImageView iv_main;
        private TextView tv_name, tv_price, tv_count;

        public HotHolder(View itemView) {
            super(itemView);
            iv_main = (ImageView) itemView.findViewById(R.id.iv_main);
            tv_name = (TextView) itemView.findViewById(R.id.tv_name);
            tv_price = (TextView) itemView.findViewById(R.id.tv_price);
            tv_count = (TextView) itemView.findViewById(R.id.tv_count);
        }
    }
}